<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * menu short summary.
 *
 * menu description.
 *
 * @version 1.0
 * @author xianbin
 */
class menu extends Menu_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->Model('Menu_model');
        $this->load->library('ajaxMsgModel');
        $this->load->library('form_validation');
    }

    public function manage()
    {

        $this->ajaxmsgmodel->Type='2';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $this->load->view('admin/menu/manage.html');
    }

    //添加一级栏目页面加载
    public function add_top_menu()
    {
        $this->ajaxmsgmodel->Type='3';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $this->load->view('admin/menu/add_top_menu.html');
    }

    //添加一级栏目
    public function ajax_add_top_menu()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $menuModel=new $this->Menu_model;
        $menuModel->parentid=$this->input->post('parentid');
        $menuModel->arrparentid=$this->input->post('arrparentid');
        $menuModel->weight=$this->input->post('weight');
        $menuModel->depth=$this->input->post('depth');
        $menuModel->child=$this->input->post('child');
        $menuModel->depth=$this->input->post('depth');
        $menuModel->isuse=$this->input->post('isuse');
        $menuModel->icon=$this->input->post('icon');
        $menuModel->title=$this->input->post('title');
        $menuModel->entitle=$this->input->post('entitle');
        $menuModel->menu_control=$this->input->post('menu_control');
        $menuModel->menu_action=$this->input->post('menu_action');
        $menuModel->page_type=$this->input->post('page_type');
        $menuModel->isdrop=$this->input->post('isdrop');
        $menuModel->arrchild='';
        $menuModel->path='';
        $res=$menuModel->insert_top_menu($menuModel);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='添加成功';
            $this->ajaxmsgmodel->Statu='ok';
        }
        else
        {
            $this->ajaxmsgmodel->Msg='添加失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    //一级栏目更改页面加载
    public function edit_top_menu()
    {
        $this->ajaxmsgmodel->Type='3';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $menuid=$this->uri->segment(4);
        $data['menu_model']=$this->Menu_model->get_top_menu($menuid);
        $this->load->view('admin/menu/edit_top_menu.html',$data);
    }
    //一级栏目修改方法
    public function ajax_edit_top_menu()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $menuModel=new $this->Menu_model;
        $menuModel->menuid=$this->input->post('menuid');
        $menuModel->title=$this->input->post('title');
        $menuModel->entitle=$this->input->post('entitle');
        $menuModel->menu_control=$this->input->post('menu_control');
        $menuModel->menu_action=$this->input->post('menu_action');
        $menuModel->page_type=$this->input->post('page_type');
        $relation=$this->input->post('relation');
        $res=$menuModel->edit_top_menu($menuModel,$relation);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='修改成功';
            $this->ajaxmsgmodel->Statu='ok';
        }
        else
        {
            $this->ajaxmsgmodel->Msg='修改失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }
    //更改栏目权重
    public function set_menu_weight()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $menuModel=new $this->Menu_model;
        $menuModel->weight=$this->input->post('weight');
        $menuModel->menuid=$this->uri->segment(4);
        $backurl=$this->input->get('returnurl');
        $res=$menuModel->set_menu_weight($menuModel);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='操作成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$backurl;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='操作失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function del_top_menu()
    {
        $menuid=$this->uri->segment(4);
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $res=$this->Menu_model->del_top_menu($menuid);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='删除成功';
            $this->ajaxmsgmodel->Statu='ok';
        }else{
            $this->ajaxmsgmodel->Msg='删除失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function check_top_menu_unique()
    {
        $val=$this->input->get('menu_control');
        $mark=$this->input->get('mark');
        $res='true';
        if($mark=='0'){
            $ures=$this->Menu_model->check_add_top_menu_controller($val);
            if($ures->menu_count>=1){
                $res='false';
            }else{
                $res='true';
            }
        }else{
            $ures=$this->Menu_model->check_eidt_top_menu_controller($val,$mark);
            if($ures->menu_count>=1){
                $res='false';
            }else{
                $res='true';
            }
        }
        echo $res;
    }

    public function index()
    {
        $this->ajaxmsgmodel->Type='1';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $data['init']=$this->input->get('init');
        $data['crumbs_items']=$this->get_crumbs_menu(0);
        $this->load->view('admin/menu/menulist.html',$data);
    }

    public function edit_top_menu_drop()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $menuModel=new $this->Menu_model;
        $menuModel->isdrop=$this->input->post('drop');
        $menuModel->menuid=$this->uri->segment(4);
        $backurl=$this->input->get('returnurl');
        $res=$menuModel->edit_top_menu_drop($menuModel);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='操作成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$backurl;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='操作失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    //子集管理
    public function child_menu()
    {
        $this->ajaxmsgmodel->Type='1';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $parent_menuid=$this->uri->segment(4);
        //p($this->get_crumbs_menu($parent_menuid));
        $data['childs']=$this->Menu_model->get_childs_by_parentid($parent_menuid);
        $data['parentid']=$parent_menuid;
        $data['crumbs_items']=$this->get_crumbs_menu($parent_menuid);
        $this->load->view('admin/menu/child_menu.html',$data);
    }

    public function add_child_menu()
    {
        $this->ajaxmsgmodel->Type='2';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $parent_menuid=$this->uri->segment(4);
        $data['parentid']=$parent_menuid;
        $data['parent_menu']=$this->Menu_model->get_menu_by_id($parent_menuid);
        //p($data['parent_menu']);
        $this->load->view('admin/menu/add_child_menu.html',$data);
    }

    public function ajax_add_child_menu()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $menuModel=new $this->Menu_model;
        $menuModel->parentid=$this->input->post('parentid');
        $menuModel->arrparentid=$this->input->post('arrparentid');
        $menuModel->weight=$this->input->post('weight');
        $menuModel->depth=$this->input->post('depth');
        $menuModel->child=$this->input->post('child');
        $menuModel->depth=$this->input->post('depth');
        $menuModel->isuse=$this->input->post('isuse');
        $menuModel->icon=$this->input->post('icon');
        $menuModel->title=$this->input->post('title');
        $menuModel->entitle=$this->input->post('entitle');
        $menuModel->menu_control=$this->input->post('menu_control');
        $menuModel->menu_action=$this->input->post('menu_action');
        $menuModel->page_type=$this->input->post('page_type');
        $menuModel->isdrop=$this->input->post('isdrop');
        $menuModel->arrchild='';
        $menuModel->path='';
        $res=$menuModel->insert_child_menu($menuModel);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='添加成功';
            $this->ajaxmsgmodel->Statu='okto';
            $this->ajaxmsgmodel->BackUrl=site_url('admin/menu/child_menu/'.$menuModel->parentid);
        }
        else
        {
            $this->ajaxmsgmodel->Msg='添加失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    //子集栏目修改
    public function edit_child_menu()
    {
        $this->ajaxmsgmodel->Type='2';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $menuid=$this->uri->segment(4);
        $data['menu_model']=$this->Menu_model->get_menu_by_id($menuid);
        //p($data['parent_menu']);
        $this->load->view('admin/menu/edit_child_menu.html',$data);
    }

    public function ajax_edit_child_menu()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $menuModel=new $this->Menu_model;
        $menuModel->menuid=$this->input->post('menuid');
        $menuModel->title=$this->input->post('title');
        $menuModel->entitle=$this->input->post('entitle');
        $menuModel->menu_control=$this->input->post('menu_control');
        $menuModel->menu_action=$this->input->post('menu_action');
        $menuModel->page_type=$this->input->post('page_type');
        $relation=$this->input->post('relation');
        $res=$menuModel->edit_child_menu($menuModel,$relation);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='修改成功';
            $this->ajaxmsgmodel->Statu='okto';
            $this->ajaxmsgmodel->BackUrl=site_url('admin/menu/child_menu/'.$menuModel->parentid);
        }
        else
        {
            $this->ajaxmsgmodel->Msg='修改失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    //删除子集栏目
    public function del_child_menu()
    {
        $menuid=$this->uri->segment(4);
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $res=$this->Menu_model->del_child_menu($menuid);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='删除成功';
            $this->ajaxmsgmodel->Statu='ok';
        }else{
            $this->ajaxmsgmodel->Msg='删除失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }


    public function menulist()
    {
        $this->ajaxmsgmodel->Type='1';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $parent_menuid=$this->uri->segment(4);
        //p($this->get_crumbs_menu($parent_menuid));
        $data['childs']=$this->Menu_model->get_childs_by_parentid($parent_menuid);
        $data['parentid']=$parent_menuid;
        $data['crumbs_items']=$this->get_crumbs_content_menu($parent_menuid);
        $this->load->view('admin/menu/content_menulist.html',$data);
    }

    //验证子集栏目添加
    // check_child_menu_unique
    public function check_child_menu_unique()
    {
        $val=$this->input->get('menu_control');
        $mark=$this->input->get('mark');
        $parentid=$this->input->get('parentid');
        $menu_model=$this->Menu_model->get_menu_by_id($parentid);
        $path_item=explode(',',$menu_model->path);
        $top_menuid=$path_item[0];
        $res='true';
        if($mark=='0'){
            $ures=$this->Menu_model->check_add_child_menu_controller($val,$top_menuid);
            if($ures->menu_count>=1){
                $res='false';
            }else{
                $res='true';
            }
        }else{
            $ures=$this->Menu_model->check_edit_child_menu_controller($val,$mark,$top_menuid);
            if($ures->menu_count>=1){
                $res='false';
            }else{
                $res='true';
            }
        }
        echo $res;
    }
}